/**
 * 列表页mixin
 */
export const listMixin = {
    data: function () {
        return {
            //是否提交中
            hasSumit: false,
            //是否加载中
            hasLoading: false,
            //where条件查询
            where: null,
            //page分页对象
            pageObj: {
                page: 1,
                pageSize: 10,
                total: 0,
                pageSizes: [5, 10, 20, 30, 40, 50]
            },
            load: null
        }
    },
    methods: {
        //条件Form转Where
        queryFormToWhere: function () {
            //清空前where
            this.where = null;
            //遍历条件表单
            for (let prop in this.formQuery) {
                //不是原型属性 并且 不为空 则移到where上
                if (!prop.hasOwnProperty(prop) && this.formQuery[prop]) {
                    if (!this.where) this.where = {};
                    this.where[prop] = this.formQuery[prop];
                }
            }
            if (this.where) this.where = { ...this.where, ...this.pageObj };
        },
        sizeChange: function (size) {
            this.pageObj.page = 1;
            this.pageObj.pageSize = size;
            this.queryFormToWhere();
            this.loadDataProxy();
        },
        //currentPage change event
        currentPageChange: function (page) {
            this.pageObj.page = page;
            this.queryFormToWhere();
            this.loadDataProxy();
        },
        loadDataProxy() {
            let pendP = this.loadData();
            setTimeout(() => {
                this.load = Loading.service({
                    target: ".container",
                    text: "努力加载中..",
                    customClass: 'list-loading'
                });
                pendP.finally(_ => {
                    this.load && this.load.close();
                });
            }, 100);
        },
        query() {
            const _this = this;
            _this.pageObj.page = 1;
            _this.queryFormToWhere();
            _this.loadDataProxy();
        }
    },
    beforeRouteLeave(to, form, next) {
        this.load && this.load.close();
        next();
    }
}

export default listMixin 